package com.molyfun.parents.activity.report;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.BaseActivity;
import com.molyfun.parents.rest.BaseResponse;
import com.molyfun.parents.rest.bean.ReportFeedbackListResponse;
import com.molyfun.parents.utils.Const;
import com.molyfun.parents.utils.UIUtils;
import com.molyfun.parents.view.common.MfEditText;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ReportFeedbackEditActivity extends BaseActivity implements View.OnClickListener{


    @InjectView(R.id.base_title_left)
    ImageView mTitleBack;
    @InjectView(R.id.base_title)
    TextView mTitle;
    @InjectView(R.id.report_feedback_text)
    MfEditText mFeedbackEdt;
    @InjectView(R.id.commit)
    TextView mCommit;

    private String mReportId;
    private ReportFeedbackListResponse.ReportFeedbackBean mReportFeedbackBean;

    public static void startActivity(Context context, String reportId, ReportFeedbackListResponse.ReportFeedbackBean reportFeedbackBean) {
        Intent intent = new Intent();
        intent.putExtra(Const.REPORT_ID, reportId);
        intent.putExtra(Const.REPORT_FEEDBACK_BEAN, reportFeedbackBean);
        intent.setClass(context, ReportFeedbackEditActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_report_feedback_edit);
        ButterKnife.inject(this);

        mTitleBack.setOnClickListener(this);

        getSupportActionBar().setTitle("");

        mReportFeedbackBean = getIntent().getParcelableExtra(Const.REPORT_FEEDBACK_BEAN);
        if(mReportFeedbackBean != null){
            mTitle.setText("编辑反馈");
            mFeedbackEdt.setText(mReportFeedbackBean.getContent());
            mCommit.setEnabled(true);
        }else{
            mTitle.setText("报告反馈");
        }
        mReportId = getIntent().getStringExtra(Const.REPORT_ID);
        mCommit.setOnClickListener(this);
        mFeedbackEdt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(s.length() > 0){
                    mCommit.setEnabled(true);
                }else{
                    mCommit.setEnabled(false);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.base_title_left:
                finish();
                break;
            case R.id.commit:
                if(TextUtils.isEmpty(mFeedbackEdt.getText().toString())){
                    UIUtils.showToast(this, "请输入反馈内容");
                    return;
                }
                mCommit.setClickable(false);
                sendFeedBack(mFeedbackEdt.getText().toString());
                break;
            default:
                break;
        }
    }

    private void sendFeedBack(final String content){

        if(mReportFeedbackBean != null){

            Call<BaseResponse> call = MFPApplication.getInstance().getRestClient().updateReportReport(mReportFeedbackBean.getId(), content);
            call.enqueue(new Callback<BaseResponse>() {
                @Override
                public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) {
                    if(response.body().getCode() == 200){
                        UIUtils.showToast(ReportFeedbackEditActivity.this, "提交成功");
                        if(mReportFeedbackCallback != null){
                            mReportFeedbackCallback.onFeedbackEdtSuccess(content);
                        }
                        finish();

                    }else{
                        UIUtils.showToast(ReportFeedbackEditActivity.this, response.body().getMsg());
                        mCommit.setClickable(true);
                    }
                }

                @Override
                public void onFailure(Call<BaseResponse> call, Throwable t) {
                    mCommit.setClickable(true);
                }
            });

        }else{
            Call<BaseResponse> call = MFPApplication.getInstance().getRestClient().addReport(mReportId, content);
            call.enqueue(new Callback<BaseResponse>() {
                @Override
                public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) {
                    if(response.body().getCode() == 200){
                        UIUtils.showToast(ReportFeedbackEditActivity.this, "提交成功");
                        if(mReportFeedbackCallback != null){
                            mReportFeedbackCallback.onFeedbackSuccess();
                        }
                        finish();

                    }else{
                        UIUtils.showToast(ReportFeedbackEditActivity.this, response.body().getMsg());
                        mCommit.setClickable(true);
                    }
                }

                @Override
                public void onFailure(Call<BaseResponse> call, Throwable t) {
                    mCommit.setClickable(true);
                }
            });
        }

    }

    public interface ReportFeedbackCallback{
        void onFeedbackSuccess();
        void onFeedbackEdtSuccess(String content);
    }

    private static ReportFeedbackCallback mReportFeedbackCallback;

    public static void setReportFeedbackCallback(ReportFeedbackCallback reportFeedbackCallback){
        mReportFeedbackCallback = reportFeedbackCallback;
    }

}
